package roborally.Conditions;
import java.util.Set;

import roborally.*;

/**
 * A class of Can-hit-robot-conditions for playing the roborally game, involving static final can-hit-robot.
 * 
 * @version  2.90
 * @author   Michiel Ruelens & Dieter Naten
 */
public class Can_hit_robot extends Condition {

	private static final Can_hit_robot chr = new Can_hit_robot();
	
	/**
	 * Create a new Can-hit-robot. This condition checks whether a given robot could hit another robot if it would shoot its laser.
	 */
	private Can_hit_robot(){
		this.setStringForm("Can-hit-robot");
	}

	/**
	 * This method is used to get the Can-hit-robot object. As there is no need for multiple objects of this class only 1 is made and always used.
	 */
	@Override
	public Can_hit_robot getInstance() {
		return chr;
	}
	
	/**
	 * Returns true if the robot could hit a robot by shooting it's laser.
	 * (There is no need for 100% chance to hit a robot! If multiple placeables (including a robot) are standing on the coordinate that would get shot, 
	 * there is a chance that shooting would hit a robot, so this method returns true.)
	 */
	@Override
	public boolean validate(Placeable placeable) {
		boolean result = false;
		if(placeable instanceof Robot){
			Orientation o = Orientation.convertIntToOrientation(((Robot) placeable).getOrientation());
			Board b = placeable.getBoard();
			Coordinate shotCoordinate = Coordinate.getCoordinateInDirectionThatContainsPlaceable(o,b,(Robot) placeable);
			Set<Placeable> shotPlaceables = b.getAllObjectsByPosition(shotCoordinate.getX(), shotCoordinate.getY());
			for(Placeable p : shotPlaceables){
				if(p instanceof Robot){
					result = true;
				}
			}
		}
		return result;
	}

}
